package com.aiyige.utils.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.utils.StringUtils;

public class LabelImageView extends FrameLayout {
    RoundCornerImageView imageView;
    TextView labelTv;
    float cornerRadius = 0;
    String labelStr = "";
    Drawable src = null;
    View labelLeftView = null;
    ImageView labelLeftIv = null;


    public LabelImageView(@NonNull Context context) {
        super(context);
        init(null);
    }

    public LabelImageView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public LabelImageView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    public TextView getLabelTv() {
        return labelTv;
    }

    public RoundCornerImageView getImageView() {
        return imageView;
    }

    public void setImageView(RoundCornerImageView imageView) {
        this.imageView = imageView;
    }

    public void setLabel(String str) {
        labelTv.setText(str);
        if (TextUtils.isEmpty(str)) {
            labelTv.setVisibility(INVISIBLE);
            labelLeftView.setVisibility(INVISIBLE);
            labelLeftIv.setVisibility(INVISIBLE);
        } else {
            labelTv.setVisibility(VISIBLE);
            labelLeftView.setVisibility(VISIBLE);
            labelLeftIv.setVisibility(VISIBLE);
        }
    }

    public String getLabel() {
        return labelTv.getText().toString();
    }


    private void init(AttributeSet attrs) {
        if (attrs != null) {
            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.LabelImageView);
            try {
                labelStr = a.getString(R.styleable.LabelImageView_liv_label_text);
            } catch (Exception e) {
            }
            try {
                cornerRadius = a.getDimension(R.styleable.LabelImageView_liv_corner_radius, 0);
            } catch (Exception e) {
            }
            try {
                src = a.getDrawable(R.styleable.LabelImageView_liv_src);
            } catch (Exception e) {
            }
            a.recycle();
        }
        labelStr = TextUtils.isEmpty(labelStr) ? "" : labelStr;
        LayoutInflater.from(getContext()).inflate(R.layout.label_image_view, this, true);
        imageView = (RoundCornerImageView) findViewById(R.id.iv);
        labelTv = (TextView) findViewById(R.id.labelTv);
        imageView.setRadius(cornerRadius);
        imageView.setImageDrawable(src);
        labelTv.setText(labelStr);
        labelLeftIv = (ImageView) findViewById(R.id.labelLeftIv);
        labelLeftView = findViewById(R.id.labelLeftView);
    }
}
